﻿using System;
using System.Globalization;
using System.Text;

namespace Jayden.Dll.Core.Logging
{
	public class SimpleFormatter : Formatter
	{
		public override string Format(LogRecord record)
		{
			StringBuilder builder = new StringBuilder();
			builder.AppendFormat
			(
				"[{0}] {1} {2} : {3}\r\n",
				record.ThreadId.ToString().PadLeft(5, '0'),
				record.DateTime.ToString(CultureInfo.InvariantCulture),
				record.Level.Name.PadLeft(10, ' '),
				record.Message
			);
			if (record.Exception != null)
				builder.Append(FormatException(record.Exception));
			return builder.ToString();
		}

		public string FormatException(Exception e)
		{
			StringBuilder builder = new StringBuilder();
			builder.AppendLine(string.Format("Exception of type {0} : '{1}'.", e.GetType(), e.Message));
			builder.AppendLine(e.StackTrace);
			if (e.InnerException != null)
			{
				builder.AppendLine("caused by:");
				builder.Append(FormatException(e.InnerException));
			}
			return builder.ToString();
		}
	}
}
